package cn.ayanami.blog.article.controller;

import jakarta.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.ayanami.blog.article.entity.dto.AddCategoryDto;
import cn.ayanami.blog.article.entity.dto.EditCategoryDto;
import cn.ayanami.blog.article.service.CategoryService;
import cn.ayanami.blog.system.bean.Result;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping(value ="/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;



    @PostMapping("/addCategory")
    public Mono<Result<String>> addCategory(@RequestBody @Valid AddCategoryDto dto, BindingResult error) {
        if(error.hasErrors()){
            return Mono.just(Result.validateError(error.getAllErrors()));
        }
        var result = categoryService.addCategory(dto);
        return result.mapNotNull(i->Result.OK(String.valueOf(i)));

    }

    
    @PostMapping("/editCategory")
    public Mono<Result<String>> renameCategory(@RequestBody @Valid EditCategoryDto dto, BindingResult error) {
        if(error.hasErrors()){
            return Mono.just(Result.validateError(error.getAllErrors()));
        }
        var result = categoryService.editCategoryDto(dto);
        return result.mapNotNull(i -> Result.OK(String.valueOf(i)));

    }



}
